package com.example.multithread.no1114;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchFoo {

    public CountDownLatchFoo() {

    }

    CountDownLatch two = new CountDownLatch(1);
    CountDownLatch three = new CountDownLatch(1);

    public static void main(String[] args) {
        CountDownLatchFoo foo = new CountDownLatchFoo();

        new Thread(() -> {
            foo.first();
        }).start();
        new Thread(() -> {
            try {
                foo.second();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                foo.third();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void first() {

        System.out.print("first");
        two.countDown();
    }

    public void second() throws InterruptedException {
        two.await();
        System.out.print("two");
        three.countDown();
    }

    public void third() throws InterruptedException {
        three.await();
        System.out.print("three");
    }
}
